<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryFormRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文章编码" prop="articleCode">
        <el-input
          v-model="queryParams.articleCode"
          placeholder="请输入文章编码"
          clearable
        />
      </el-form-item>
      <el-form-item label="APP" prop="appKey">
        <el-select v-model="queryParams.appKey" placeholder="请选择APP应用" filterable clearable>
          <el-option
          v-for="item in appKeyOptions"
          :key="item.id"
          :label="item.appName" 
          :value="item.appKey"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="位置" prop="locationCode">
        <el-select v-model="queryParams.locationCode" placeholder="请选择位置" filterable clearable>
          <el-option
          v-for="item in articleLocationOptions"
          :key="item.id"
          :label="item.articleName" 
          :value="item.locationCode"
          ></el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['content:articleItem:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="small"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['content:articleItem:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['content:articleItem:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          @click="handleExport"
          v-hasPermi="['content:articleItem:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="articleItemList"
      :default-sort="tableSort"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="文章编码" align="center" prop="articleCode" />
      <el-table-column label="文章名称" align="center" prop="articleName" />
      <el-table-column label="应用" align="center" prop="appName" />
      <el-table-column label="位置" align="center" prop="locationName" />
      <!-- <el-table-column label="文章类型" align="center" prop="articleType" /> -->
      <el-table-column label="标题" align="center" prop="title" />
      <!-- <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="排序" align="center" prop="sortNum" /> -->
      <el-table-column label="状态" align="center" prop="status" >
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" sortable="custom">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['content:articleItem:edit']"
          >修改</el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['content:articleItem:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page="queryParams.pageNum"
      :limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文章对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px" append-to-body  >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="APP" prop="appKey">
          <el-select v-model="form.appKey" placeholder="请选择位置" filterable clearable>
          <el-option
          v-for="item in appKeyOptions"
          :key="item.id"
          :label="item.appName" 
          :value="item.appKey"
          ></el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="位置编码" prop="locationCode">
          <el-select v-model="form.locationCode" placeholder="请选择位置" filterable clearable>
            <el-option
            v-for="item in articleLocationOptions"
            :key="item.id"
            :label="item.articleName" 
            :value="item.locationCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文章编码" prop="articleCode">
          <el-input v-model="form.articleCode" placeholder="请输入文章编码" />
        </el-form-item>
        <el-form-item label="文章名称" prop="articleName">
          <el-input v-model="form.articleName" placeholder="请输入文章名称" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容">
          <el-input type="textarea" v-model="form.content" />
        </el-form-item>
        <!-- <el-form-item label="排序" prop="sortNum">
          <el-input v-model="form.sortNum" placeholder="请输入排序" />
        </el-form-item> -->
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in sys_normal_disable"
              :key="dict.value"
:label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
          <div class="dialog-footer">
            <el-button @click="handleDialogCancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
          </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ArticleItem">
import { ref, toRefs } from "vue";
import { listArticleItem, getArticleItem, delArticleItem, addArticleItem, updateArticleItem, exportXlsUrl } from "@/api/content/articleItem";
import { getArticleLocationSelectList } from "@/api/content/articleLocation"
import { getAppKeySelectList } from "@/api/content/appInfo"

const { proxy } = getCurrentInstance();

const { sys_normal_disable } = proxy.useDictNum("sys_normal_disable");

  // 遮罩层
  const loading = ref(true);
  // 选中数组
  const ids = ref([]);
  // 非单个禁用
  const single = ref(true);
  // 非多个禁用
  const multiple = ref(true);
  // 显示搜索条件
  const showSearch = ref(true);
  // 总条数
  const total = ref(0);
  // 文章表格数据
  const articleItemList  = ref([]);
  // 弹出层标题
  const dialogTitle  = ref("");
  // 是否显示弹出层
  const dialogVisible  = ref(false);

// 位置下拉选项
  const articleLocationOptions = ref([]);
  // appkey下拉选项
  const appKeyOptions = ref([]);

  // 查询参数
     const reactiveData  = reactive(
     {
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          locationCode: null,
          articleCode: null,
          articleName: null,
          title: null,
          status: null,
            params: {
              sort: 'createTime',
              order: 'descending',
            }
        },
        form: {},
        rules: {
        appKey: [
          { required: true, message: "APP不能为空", trigger: "blur" }
        ],
        locationCode: [
          { required: true, message: "位置编码不能为空", trigger: "blur" }
        ],
        articleCode: [
          { required: true, message: "文章编码不能为空", trigger: "change" }
        ],
        articleName: [
          { required: true, message: "文章名称不能为空", trigger: "change" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        }
     });

      const {queryParams,form,rules} = toRefs(reactiveData);

      const tableSort = computed(()=>{
        if(queryParams.value.params.sort){
          return {
            prop: queryParams.value.params.sort,
            order: queryParams.value.params.order
          }
        }
        return null;
      })

    /** 查询文章列表 */
   function getList() {
     loading.value = true;
      listArticleItem(queryParams.value).then(response => {
        articleItemList.value = response.data.records;
        total.value = response.data.total;
        loading.value = false;
      });
    }
    // 取消按钮
    function handleDialogCancel() {
      dialogVisible.value = false;
      reset();
    }
    // 表单重置
    function reset() {
      form.value = {
        id: null,
        appKey: null,
        locationCode: null,
        articleCode: null,
        articleName: null,
        articleType: null,
        title: null,
        content: null,
        sortNum: null,
        status: 0,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        yn: null
      };
      proxy.resetForm("formRef");
    }
    /** 搜索按钮操作 */
   function handleQuery() {
      queryParams.value.pageNum = 1;
      getList();
    }
    /** 重置按钮操作 */
    function resetQuery() {
      proxy.resetForm("queryFormRef");
      handleQuery();
    }

    // 多选框选中数据
    function handleSelectionChange(selection) {
      ids.value = selection.map(item => item.id)
      single.value = selection.length!==1
      multiple.value = !selection.length
    }

    // 排序
    function handleSortChange({ column, prop, order }){
      queryParams.value.params.sort = prop?prop:null;
      queryParams.value.params.order = order?order:null;
      getList();
    }

    /** 新增按钮操作 */
    function handleAdd() {
      reset();
      dialogVisible.value = true;
      dialogTitle.value = "添加文章";
    }

    /** 修改按钮操作 */
    function handleUpdate(row) {
      reset();
      const id = row.id || ids.value
      getArticleItem(id).then(response => {
        form.value = response.data;
        dialogVisible.value = true;
        dialogTitle.value = "修改文章";
      });
    }
    /** 提交按钮 */
    function submitForm() {
      proxy.$refs["formRef"].validate(valid => {
        if (valid) {
          if (form.value.id != null) {
            updateArticleItem(form.value).then(response => {
              proxy.$modal.msgSuccess("修改成功");
              dialogVisible.value = false;
              getList();
            });
          } else {
            addArticleItem(form.value).then(response => {
              proxy.$modal.msgSuccess("新增成功");
              dialogVisible.value = false;
              getList();
            });
          }
        }
      });
    }
    /** 删除按钮操作 */
   function handleDelete(row) {
      const ids = row.id || ids.value;
      proxy.$modal.confirm('是否确认删除文章编号为"' + ids + '"的数据项？').then(function() {
        return delArticleItem(ids);
      }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
   /** 导出按钮操作 */
   function handleExport() {
      proxy.download(exportXlsUrl, {
        ...queryParams.value
      }, `articleItem_${new Date().getTime()}.xlsx`)
    }

  function getArticleLocationOptions(){
    getArticleLocationSelectList().then(res=>{
      articleLocationOptions.value = res.data;
    })
  }

  function getAppKeyOptions(){
    getAppKeySelectList().then(res=>{
      appKeyOptions.value = res.data;
    })
  }

   /** 初始化数据 **/
   getList();
   getArticleLocationOptions();
   getAppKeyOptions();
</script>
